/*
Copyright 2005-2014 CP Fricano - All Rights Reserved

This software and source code is licensed under the Apache
License, Version 2.0. You may not use this software or source
code except in compliance with the License.  You may obtain a
copy of the License at: http://www.apache.org/licenses/LICENSE-2.0.
Use and distributed under this License is on an "as is" basis,
without expressed or implied warranties or conditions of any kind.
See the License for the specific language governing permissions and
limitations.
 */

package com.fricano.java42.swing.dings.tester;

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTree;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.event.EventListenerList;

import com.fricano.java42.sequencer.J42Helper;
import com.fricano.java42.sequencer.J42Listener;
import com.fricano.java42.sequencer.J42Sequencer;
import com.fricano.java42.swing.dings.J42SwingDingController_SlideWindow.Alignment;
import com.fricano.java42.swing.dings.J42SwingDingManager_SlideWindow;
import com.fricano.java42.swing.dings.J42SwingDing_Factory;

final public class J42SwingDing_SlidableTest_01 {

    final private static Border border_01 = new CompoundBorder(new MatteBorder(10, 10, 10, 10, new Color(0, 0, 0)), new CompoundBorder(new LineBorder(new Color(192, 192, 192), 2), new LineBorder(new Color(64, 64, 64), 3)));
    final private static Border border_02 = new CompoundBorder(new MatteBorder(10, 10, 10, 10, new Color(0, 0, 0)), new CompoundBorder(new LineBorder(new Color(192, 192, 192), 2), new CompoundBorder(new LineBorder(new Color(0, 0, 0), 2), new EmptyBorder(4, 4, 4, 4))));

    static J42SwingDing_Factory controller;

    static public void main(final String[] args) throws Exception {

        final int id = 0x01;
        // final int id = 0x02;
        // final int id = 0x03;
        // final int id = 0x04;

        final J42Sequencer<JFrame> j42Sequencer = new J42Sequencer<JFrame>() {

            @Override
            public void doSequence0_CreateComponents(final JFrame frame, final Container contentPane, final J42Helper<JFrame> helper) {
                controller = new J42SwingDing_Factory(frame);
            }

            @Override
            public void doSequence1_AddComponents(final JFrame frame, final Container contentPane, final J42Helper<JFrame> helper) {

                switch (id) {
                    case 0x01: {
                        final JComponent jButton = new JButton("JButton_01");
                        jButton.setSize(100, 50);
                        final J42SwingDingManager_SlideWindow manager = controller.create_SlideManager();
                        manager.attachSlider(Alignment.EXTERIOR_RIGHT, jButton, 42, 500, 0, 0);
                        final JButton b = new JButton("Slide");
                        // v1.8
                        // b.addActionListener(e -> manager.slideToggle(true));
                        b.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(final ActionEvent e) {
                                manager.slideToggle(true);
                            }
                        });
                        contentPane.add(b);
                        break;
                    }

                    case 0x02: {
                        final JComponent jComponent = new JTree();
                        jComponent.setSize(300, 200);
                        jComponent.setBorder(new LineBorder(Color.GRAY, 2));
                        final J42SwingDing_Factory controller = new J42SwingDing_Factory(frame);
                        final J42SwingDingManager_SlideWindow manager = controller.create_SlideManager();
                        manager.attachSlider(Alignment.EXTERIOR_RIGHT, jComponent, 42, 500, 0, 0);
                        helper.addJButton(contentPane, "SLIDE-02", new J42Listener<JButton, ActionEvent>() {
                            @Override
                            public void doWorkOutsideEDT(final JButton t, final ActionEvent eventObject) throws Exception {
                                if (manager.isClosed()) {
                                    manager.slideOpen(true);
                                }
                                else {
                                    manager.slideClosed(true);
                                }
                            }

                            @Override
                            public void doWorkInsideEDT(final JButton t, final ActionEvent eventObject) throws Exception {
                            }
                        });
                        break;
                    }

                    case 0x03: {
                        final JComponent anchorButton = helper.addJButton(contentPane, "Anchor-01", null);
                        final JComponent jButton = new JButton("JButton_03");
                        jButton.setSize(100, 50);
                        final J42SwingDing_Factory controller = new J42SwingDing_Factory(frame);
                        final J42SwingDingManager_SlideWindow manager = controller.create_SlideManager();
                        manager.attachSlider(Alignment.EXTERIOR_RIGHT, jButton, 42, 500, 0, 0);
                        helper.addJButton(contentPane, "SLIDE-03", new J42Listener<JButton, ActionEvent>() {
                            @Override
                            public void doWorkOutsideEDT(final JButton t, final ActionEvent eventObject) throws Exception {
                                if (manager.isClosed()) {
                                    manager.slideOpen(true);
                                }
                                else {
                                    manager.slideClosed(true);
                                }
                            }

                            @Override
                            public void doWorkInsideEDT(final JButton t, final ActionEvent eventObject) throws Exception {
                            }
                        });
                        break;
                    }

                    case 0x04: {
                        break;
                    }
                    default:
                        break;
                }
            }

            @Override
            public void doSequence2_LocateSizePackShow(final JFrame window, final Container contentPane, final J42Helper<JFrame> helper) {
                window.setLocationRelativeTo(null);
                window.setVisible(true);
            }

            @Override
            public void doSequence3_StartApplication(final JFrame window, final Container contentPane, final J42Helper<JFrame> helper) {
                if (id == 0x04) {
                    window.dispose();
                    createSample_001();
                }
            }

            @Override
            public void doSequence9_StopApplication(final JFrame window, final Container contentPane, final J42Helper<JFrame> helper) {

            }

        };

        final EventListenerList l = new EventListenerList();

    }

    private static void createSample_001() {

        final JTree jSlider = new JTree();
        jSlider.setSize(96, 96);
        jSlider.setBorder(border_02);

        final JFrame jFrame = new JFrame();
        final JMenuBar jMenuBar = new JMenuBar();
        jFrame.setJMenuBar(jMenuBar);
        final JMenu jMenu = new JMenu();
        jMenuBar.add(jMenu);
        final JMenuItem jMenuItem = new JMenuItem("LL");
        jMenu.add(jMenuItem);
        jFrame.setSize(400, 275);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jFrame.setVisible(true);

        final J42SwingDing_Factory controller = new J42SwingDing_Factory(jFrame);
        final J42SwingDingManager_SlideWindow manager = controller.create_SlideManager();
        while (true) {
            for (final Alignment alignment : Alignment.values()) {
                manager.attachSlider(alignment, jSlider, 100, 500, 0, 0);
                sleep(50);
                manager.slideOpen(true);
                sleep(500);
                manager.slideClosed(true);
                // sleep(500);
                manager.slideOpen(true);
                // sleep(500);
                manager.slideClosed(true);
                // sleep(500);
                manager.detachSlider(jSlider);
                // sleep(500);
                if (!manager.isReady()) {
                    break;
                }
            }
            if (!manager.isReady()) {
                break;
            }
        }

        // manager.attachSlider(Alignment.INTERIOR_BOTTOM, jSlider, 42, 500, 0, 0);
        // manager.attachSlider(Alignment.INTERIOR_LEFT, jSlider, 42, 500, 0, 0);
        // manager.attachSlider(Alignment.INTERIOR_RIGHT, jSlider, 42, 500, 0, 0);
        // manager.attachSlider(Alignment.EXTERIOR_TOP, jSlider, 42, 500, 0, 0);
        // manager.attachSlider(Alignment.EXTERIOR_BOTTOM, jSlider, 42, 500, 0, 0);
        // manager.attachSlider(Alignment.EXTERIOR_LEFT, jSlider, 42, 500, 0, 0);
        // manager.attachSlider(Alignment.EXTERIOR_RIGHT, jSlider, 42, 500, 0, 0);
        // manager.addDevelopmentControls("JTree");

        jFrame.dispose();
    }

    private static void sleep(final long durationMS) {
        try {
            Thread.sleep(durationMS);
        } catch (final InterruptedException e) {
        }
    }
}
